package practice1_100;

import java.util.List;

public class Practice92 {
        public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left==right) return head;
        ListNode temp =head;
        ListNode tempNext;
        //left为1时候
        if(left==1){
            for (int i=0;right-left>i;i++){
                ListNode tempone=temp.next;
                temp.next=temp.next.next;
                tempone.next=head;
                head=tempone;
            }
            return head;
        }else if(left==2) {
            temp=temp.next;
            for (int i=0;right-left>i;i++){
                ListNode a=temp.next;
                temp.next=temp.next.next;
                a.next=head.next;
                head.next=a;
            }
            return head;
        }else {
            tempNext=temp.next;
            for (int i = 1; i < left-1; i++) {
                temp=temp.next;
                tempNext=temp.next;
            }
            for (int i = 0; i < right-left; i++) {
                //tempNext=tempNext.next;
                ListNode a=tempNext.next;
                tempNext.next=tempNext.next.next;
                a.next=temp.next;
                temp.next=a;
            }
            return head;
        }
    }

    //反转链表
    public ListNode re(ListNode head){
        if(head.next==null) return head;
        ListNode last= re(head.next);//此次递归
        head.next.next=head;//将当前的head放到head.next的后面
        head.next=null;//将当前head的next置空
        return  last;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        ListNode a=new Practice92().reverseBetween(head,2,4);
        System.out.println(a);
    }
}
